#include "fig_boss_laser.h"
#include "fig_enemy_boss.h"
#include "fighter.h"
#include "fig_player_craft.h"
#include "fig_bonepart.h"
#include "fig_sounds.h"
#include "fig_constants.h"

#include "math/intersection.h"
#include "dd/dd_man.h"


FIGBossLaser::FIGBossLaser(FIGEnemyBoss*	pBoss,
						   FIGBonePart*		pWarningBonepart1,
							FIGBonePart*	pWarningBonepart2,
							float			SclaeWithoutGlow,
							COLOR		c,
							float		LaserScale) : 
								m_pBoss(pBoss),
								m_pWarningBonepart1(pWarningBonepart1),
								m_pWarningBonepart2(pWarningBonepart2),
								m_SclaeWithoutGlow(SclaeWithoutGlow),
								m_Color(c),
								m_LaserScale(LaserScale),
								m_LaserState(LASER_STATE_NONE),
								m_LaserWidthScale(0),
								m_LaserSpritePosY(0)
{
	m_LaserWarningCounter.SetLimit(1.5f);
	m_LaserWarningCounter.ForceReady();	 

	m_SpriteLaser.Create("hummer_lazer");
	m_SizeLaser = m_SpriteLaser.m_Size;
	
    if(pWarningBonepart1)
        pWarningBonepart1->SetShouldDraw(false);
    
    if(pWarningBonepart2)
        pWarningBonepart2->SetShouldDraw(false);
}


void	FIGBossLaser::Tick(float dt,
						   const math::Vec2& LaserShootPos1,
						   const math::Vec2& LaserShootPos2

						   )
{
    
    m_SpriteLaser.Tick(dt);
    
	m_LaserShootPos1 = LaserShootPos1;
	m_LaserShootPos2 = LaserShootPos2;


	

    if(m_pWarningBonepart1)
        m_pWarningBonepart1->SetPosition(m_LaserShootPos1);
    
    if(m_pWarningBonepart2)
        m_pWarningBonepart2->SetPosition(m_LaserShootPos2);


	m_LaserSpritePosY += 1.0f * dt;
	if(m_LaserSpritePosY > 1.0f)
		m_LaserSpritePosY = 0.0f;

	if(m_LaserState == LASER_STATE_WARNING)
	{
		if(!m_LaserWarningCounter.IsReady())
		{
			m_LaserWarningCounter.Tick(dt);

			float V = m_LaserWarningCounter.GetProgress();
			if( (V > 0.2f && V < 0.3f) || 
				(V > 0.6f && V < 0.7f) ||
				(V > 0.9f && V < 1.0f)
				)
			{
				if(m_pWarningBonepart1)
					m_pWarningBonepart1->SetShouldDraw(false);
				if(m_pWarningBonepart2)
					m_pWarningBonepart2->SetShouldDraw(false);
				
			}
			else
			{
				if(m_pWarningBonepart1)
					m_pWarningBonepart1->SetShouldDraw(true);
				if(m_pWarningBonepart2)
					m_pWarningBonepart2->SetShouldDraw(true);				
			}
			

		}
		else
		{
			if(m_pWarningBonepart1)
				m_pWarningBonepart1->SetShouldDraw(true);
			if(m_pWarningBonepart2)
				m_pWarningBonepart2->SetShouldDraw(true);

			m_LaserState = LASER_STATE_EXPANDING;
			FIGSounds::Instance().PlaySoundSFX(FIGSounds::BOSS_LASER);
		}
	}

	else if(m_LaserState == LASER_STATE_EXPANDING )
	{
		m_LaserWidthScale += dt;
		if(m_LaserWidthScale >= 1)
		{
			m_LaserWidthScale = 1.0f;
			m_LaserState = LASER_STATE_SHOOING;
			m_LaserShootTime = 2.0f;
		}
		
	}
	else if(m_LaserState == LASER_STATE_SHOOING )
	{
		m_LaserShootTime -= dt;
		if(m_LaserShootTime <= 0)
			m_LaserState = LASER_STATE_SHRINKING;
	}
	else if(m_LaserState == LASER_STATE_SHRINKING )
	{
		m_LaserWidthScale -= dt;
		if(m_LaserWidthScale <= 0)
		{
			m_LaserWidthScale = 0.0f;
			m_LaserState = LASER_STATE_NONE;
            if(m_pWarningBonepart1)
                m_pWarningBonepart1->SetShouldDraw(false);
			
            if(m_pWarningBonepart2)
                m_pWarningBonepart2->SetShouldDraw(false);
		}
	}

	HitTest_Laser();
}
	
void	FIGBossLaser::Render(const math::Vec2& Offset,float Scale)
{
	math::Vec2 Pos;
	
	Pos = m_LaserShootPos1;
	Pos.x -= m_SizeLaser.x * 0.5f * Scale * m_LaserWidthScale * m_LaserScale;
	m_SpriteLaser.m_Size = m_SizeLaser * Scale;
	m_SpriteLaser.m_Size.x *= (m_LaserWidthScale * m_LaserScale);
	m_SpriteLaser.m_Size.y = 1.1f;

	m_SpriteLaser.m_Color = m_Color;
	
	m_SpriteLaser.m_Pos =	m_pBoss->GetPos() + 
							Pos + 
							Offset + 
							math::Vec2(0,m_LaserSpritePosY);
	
	m_SpriteLaser.RenderClipped(
								math::Vec2(0.0f,m_pBoss->GetPos().y + Pos.y),
								math::Vec2(1.0f,1.0f)
								
								);

	m_SpriteLaser.m_Pos =	m_pBoss->GetPos() + 
							Pos + 
							Offset + 
							math::Vec2(0,m_LaserSpritePosY - m_SpriteLaser.m_Size.y);
	
	m_SpriteLaser.RenderClipped(
								math::Vec2(0.0f,m_pBoss->GetPos().y + Pos.y),
								math::Vec2(1.0f,1.0f)
								
								);
	

	//------------------------------------
	// left laser 
	Pos = m_LaserShootPos2;
	Pos.x -= m_SizeLaser.x * 0.5f * Scale * m_LaserWidthScale * m_LaserScale;
	m_SpriteLaser.m_Pos =  m_pBoss->GetPos() + Pos + Offset + math::Vec2(0,m_LaserSpritePosY);
	m_SpriteLaser.RenderClipped(
								math::Vec2(0.0f,m_pBoss->GetPos().y + Pos.y),
								math::Vec2(1.0f,1.0f)
								
								);

	m_SpriteLaser.m_Pos =	m_pBoss->GetPos() + 
							Pos + 
							Offset + 
							math::Vec2(0,m_LaserSpritePosY -  m_SpriteLaser.m_Size.y);
	
	m_SpriteLaser.RenderClipped(
								math::Vec2(0.0f,m_pBoss->GetPos().y + Pos.y),
								math::Vec2(1.0f,1.0f)
								
								);
}

void	FIGBossLaser::RenderDebug(const math::Vec2& Offset,float Scale)
{
	if(m_LaserState == LASER_STATE_WARNING)
		return;

	if( m_LaserWidthScale <= 0)
		return;   

	math::Vec2 Pos,Bottom;
	Pos = m_LaserShootPos1;
	Pos.x -=	m_SizeLaser.x * 
				0.5f * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	Pos =	m_pBoss->GetPos() + 
			Pos + 
			Offset;

	Bottom.y = 1.0f;
	Bottom.x = Pos.x +	
				m_SizeLaser.x * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;;

	dd::Manager&	DD = dd::Manager::Instance();
	DD.AddBox2D(Pos,Bottom,ColorfRGBA(1.0f,0,0,1.0f));


	Pos = m_LaserShootPos2;
	Pos.x -=	m_SizeLaser.x * 
				0.5f * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	Pos =	m_pBoss->GetPos() + 
			Pos + 
			Offset;

	Bottom.y = 1.0f;
	Bottom.x =	Pos.x +	
				m_SizeLaser.x * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow*
				m_LaserScale;

	DD.AddBox2D(Pos,Bottom,ColorfRGBA(1.0f,0,0,1.0f));

}

void	FIGBossLaser::Reset()
{
	m_pWarningBonepart1->SetShouldDraw(false);
	m_pWarningBonepart2->SetShouldDraw(false);
	m_LaserState = LASER_STATE_NONE;
}   

void	FIGBossLaser::HitTest_Laser()
{
	

	if(m_LaserState == LASER_STATE_NONE)
		return;

	if(m_LaserState == LASER_STATE_WARNING)
		return;

	if( m_LaserWidthScale <= 0)
		return;

	float Scale = Fighter::Instance().GetScale();

	math::Vec2 Pos,Bottom;
	Pos = m_LaserShootPos1;
	Pos.x -=	m_SizeLaser.x * 
				0.5f * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	Pos =	m_pBoss->GetPos() + 
			Pos;
			

	Bottom.y = 1.0f;
	Bottom.x = Pos.x +	
				m_SizeLaser.x * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	math::Vec2 PlayerPos = FIGPlayerCraft::Instance().GetPos();
	math::Vec2 PlayerHalfSize = FIGPlayerCraft::Instance().GetSize() * 0.5f * Scale;


	if( PlayerPos.x + PlayerHalfSize.x > Pos.x &&
		PlayerPos.y - PlayerHalfSize.y > Pos.y &&
		PlayerPos.y + PlayerHalfSize.y < Bottom.y &&
		PlayerPos.x - PlayerHalfSize.x < Bottom.x )
		
	{
		FIGPlayerCraft::Instance().GotDamage(0.1f,NULL, false);
		return;
	}

	Pos = m_LaserShootPos2;
	Pos.x -=	m_SizeLaser.x * 
				0.5f * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	Pos =	m_pBoss->GetPos() + 
			Pos;
			

	Bottom.y = 1.0f;
	Bottom.x =	Pos.x +	
				m_SizeLaser.x * 
				Scale * 
				m_LaserWidthScale *
				m_SclaeWithoutGlow *
				m_LaserScale;

	if( PlayerPos.x + PlayerHalfSize.x > Pos.x &&
		PlayerPos.y - PlayerHalfSize.y > Pos.y &&
		PlayerPos.y + PlayerHalfSize.y < Bottom.y &&
		PlayerPos.x - PlayerHalfSize.x < Bottom.x )
		
	{
		FIGPlayerCraft::Instance().GotDamage(1.3f,NULL, false);
		return;
	}
	

}

void	FIGBossLaser::StartWarningAndShoot()
{
	m_LaserState = LASER_STATE_WARNING;	
	m_LaserWarningCounter.Reset();
	m_LaserSpritePosY = 0;
	m_LaserWidthScale = 0;
	
}
